﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shumei.EFCore.Model;
using Shumei.EFCore.SimpleService;

namespace source.Areas.Admin.Controllers
{
    public class ProductsController : Controller
    {
        ProductService productService;

        public ProductsController()
        {
            productService = new ProductService();
        }
        /// <summary>
        /// 获取作品列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public ActionResult ProductsList(int page=1,int num = 10)
        {
            int totalPage;
            var res = productService.GetProductsList(page, out totalPage, num);
            ViewBag.page = page;
            ViewBag.totalPage = totalPage;
            return View("ProductsList", res);
        }

        /// <summary>
        /// 打开作品上传页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UploadProduct()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddProduct(Product newModel)
        {
            newModel.UploadTime = DateTime.Now;
            //newModel.user = AccountController.GetUserInfo(this.HttpContext);
            try
            {
                productService.Add(newModel, true);
                return RedirectToAction("ProductsList");
            }
            catch
            {

            }
            return null;

        }

        public ActionResult EditProduct(int id)
        {
            var _product = productService.GetProductDetail(id);
            if (productService != null)
            {
                return View("EditProduct",_product);
            }
            return null;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditProduct(Product model)
        {
            productService.Update(model, true);
            return RedirectToAction("ProductsList");
        }

        public ActionResult DeleteProduct(int id)
        {
            try
            {
                var res = productService.GetProductDetail(id);
                if (res != null)
                    productService.Delete(res);
                return RedirectToAction("ProductsList");
            }
            catch
            {
 
            }
            return null;
        }
        /// <summary>
        /// 获取作品类型
        /// </summary>
        /// <returns></returns>
        public ActionResult GetProductType()
        {
            Product_TypeService news_categoryService = new Product_TypeService();
            return Json(news_categoryService.getList(), JsonRequestBehavior.AllowGet);
        }
	}
}